const Controller = require('egg').Controller;

class SwiperController extends Controller {
  async getSwiper() {
    const { ctx } = this;
    const swiperList = await ctx.service.swiper.getSwiper()
    if(swiperList) {
      ctx.body = {
        code: 20000,
        data: swiperList
      }
    }else{
      ctx.body = {
        code: 50000,
        data: '服务器异常'
      }
    }
  }

  async index() {
    const { ctx } = this;
    let res = await ctx.service.swiper.getSwiperList()
    if(res) {
      ctx.body = {
        code: 20000,
        data: res
      }
    }
  }

  async create() {
    const { ctx } = this;
    let body = ctx.request.body
    console.log(body);
    let res = await ctx.service.swiper.postSwiper(body)
    if(res) {
      ctx.body = {
        code: 20000,
        data: res
      }
    }else{
      ctx.body = {
        code:50000,
        data:'服务器异常'
      }
    }
  }

  async destroy() {
    const { ctx } = this
    let id = ctx.params.id
    // console.log(id);
    let res = await ctx.service.swiper.deleteSwiper(id)
    if(res) {
      ctx.body = {
        code:20000,
        data:'删除成功'
      }
    }else{
      ctx.body = {
        code:50000,
        data:'服务器异常'
      }
    }
  }
}

module.exports = SwiperController;